using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Generic;
using System.Collections;
using WidgetInterface;

/// <summary>
/// Author: AustralTek 
/// Create date: 2010/05/04
/// http://www.australtek.com
/// Copyright: 2009 - 2010
/// </summary>
/* /////////////////////////////////////////////////////////////////////////////// */
namespace IndustrialDashboard
{
    [Serializable]
    public class IndustrialSidebarMenu:IWidget
    {
        #region "Variables"
        private static String strSource = "Sidebar   ";
        public String AccordionName;
        private String AppCode;
        public List<MenuItem> MenuItems;  
        #endregion
        #region "Classes"
        /// <summary>
        /// M1
        /// </summary>
        public class MenuItem
        {
            public MenuItem(Int32 _id, String _AppCode ,String _URL, String _Name, String _Target,String _Image, bool _enabled, bool _visible)
            {
                //
                //TODO: Add constructor logic here
                //
                Logger.Info(strSource, "Initializing");
                ID = _id;
                AppCode = _AppCode;
                URL = _URL;
                Target = _Target;               
                Enabled = _enabled;
                Visible = _visible;
                Name = _Name;
                Image = _Image;
                MenuDefinitions = new List<MenuDefinition> { };
            }
            public MenuItem()
            {
                //
                //TODO: Add constructor logic here
                //
                Logger.Info(strSource, "Initializing");
                MenuDefinitions = new List<MenuDefinition> { };
            }
            public List<MenuDefinition> MenuDefinitions { get; set; }
            public Int32 ID { get; set; }
            public String URL { get; set; }
            public String Image {get;set;}
            public String Target { get; set; }
            public String AppCode { get; set; }
            public bool Enabled { get; set; }
            public bool Visible { get; set; }
            public String Name { get; set; }
        }
        /// <summary>
        /// M2
        /// </summary>
        public class MenuDefinition
        {            
            private string sReference = "";
            public MenuDefinition(Int32 _id, String _AppCode,String _URL,String _Name, String _Target, String _Image, bool _enabled, bool _visible)
            {
                //
                //TODO: Add constructor logic here.
                //
                Logger.Info(strSource, "Initializing");
                ID = _id;
                AppCode = _AppCode;
                URL = _URL;
                Name = _Name;
                Target = _Target;               
                Image = _Image;           
                Enabled = _enabled;
                Visible = _visible;                
                PopupItems = new List<PopupItem> { };

            }
            public MenuDefinition()
            {
                //
                //TODO: Add constructor logic here.
                //
                Logger.Info(strSource, "Initializing");
                PopupItems = new List<PopupItem> { };
            }
            public List<PopupItem> PopupItems { get; set; }
            public Int32 ID { get; set; }
            public String AppCode { get; set; }
            public String URL { get; set; }
            public String Target { get; set; }
            public String Image { get; set; }
            public String Name { get; set; }
            public bool Enabled { get; set; }
            public bool Visible { get; set; }

        }
        /// <summary>
        /// M3
        /// </summary>
        public class PopupItem
        {            
            public PopupItem(Int32 _id,String _AppCode, String _URL,String _Name ,String _Target, String _Image, bool _enabled, bool _visible)
            {
                //
                //TODO: Add contructor logic here.
                //
                Logger.Info(strSource, "Initializing");
                ID = _id;
                AppCode = _AppCode;
                URL = _URL;
                Target = _Target;
                Name =_Name;
                Image = _Image;
                Enabled = _enabled;
                Visible = _visible;
                SubPopupItems = new List<PopupItem> { };
            }
            public PopupItem()
            {
                //
                //TODO: Add contructor logic here.
                //
                Logger.Info(strSource,"Initializing");
                SubPopupItems = new List<PopupItem> { };
            }
            public List<PopupItem> SubPopupItems { get; set; }
            public Int32 ID { get; set; }
            public String AppCode { get; set; }
            public String URL { get; set; }
            public String Target { get; set; }          
            public String Image { get; set; }
            public String Name { get; set; }
            public bool Enabled { get; set; }
            public bool Visible { get; set; }
        }
        #endregion       
        #region "Constructor"
        public IndustrialSidebarMenu()
        {
            //
            // TODO: Add constructor logic here
            //
            Logger.Info(strSource, "Initializing");
            MenuItems = new List<MenuItem> { };
        }
        public IndustrialSidebarMenu(DataSet ds) 
        {
            //
            // TODO: Add constructor logic here
            //
            Logger.Info(strSource, "Initializing");
            MenuItems = new List<MenuItem> { };
            this.FillSidebarMenu(ds);
        }
        #endregion
        #region "Methods"
        private MenuItem getMenuItem(int value)
        {
            foreach (MenuItem mi in MenuItems)
            {
                if (mi.ID == value)
                    return mi;
            }
            return null;
        }
        private MenuDefinition getMenuDefinition(int value)
        {
            foreach (MenuItem mi in MenuItems)
            {            
                foreach (MenuDefinition md in mi.MenuDefinitions){
                    if (md.ID == value)
                        return md;
                }
            }
            return null;

        }
        private PopupItem getPopupItem(int value)
        {
            foreach (MenuItem mi in MenuItems){            
                foreach (MenuDefinition md in mi.MenuDefinitions){                
                    foreach (PopupItem pi in md.PopupItems){                    
                        if (pi.ID == value)
                            return pi;
                    }
                }
            }
            return null;

        }
        public void FillSidebarMenu(DataSet ds)
        {
            String strMethod="FillSidebarMenu()";
            try
            {
                //AccordionMenuSearch.createInstanceOfDictionary();
                ////_AccordionMenu = new AccordionMenu();
                ////_AccordionMenu.AccordionName = "sideBar";
                AccordionName = "SideBarMenu";
                //AccordionMenuSearch.clearTokenList();
                ////Root Elements
                if (ds.Tables.Count > 0)
                {
                    int iID = ds.Tables[0].Columns["ID"].Ordinal;
                    int iURL = ds.Tables[0].Columns["URL"].Ordinal;
                    int iTarget = ds.Tables[0].Columns["Target"].Ordinal;                                         
                    int iEna = ds.Tables[0].Columns["Enabled"].Ordinal;
                    int iNam = ds.Tables[0].Columns["Name"].Ordinal;
                    int iApp = ds.Tables[0].Columns["AppCode"].Ordinal;
                    int iVis = ds.Tables[0].Columns["Visible"].Ordinal;
                    int iImg = ds.Tables[1].Columns["Image"].Ordinal;

                    foreach (DataRow dr in ds.Tables[0].Rows){                    
                        MenuItem mi = new MenuItem( Convert.ToInt32(dr.ItemArray[iID])
                                                    ,Convert.ToString(dr.ItemArray[iApp])
                                                    ,Convert.ToString(dr.ItemArray[iURL]).Trim()
                                                    ,Convert.ToString(dr.ItemArray[iNam])
                                                    ,Convert.ToString(dr.ItemArray[iTarget]).Trim()
                                                    ,Convert.ToString(dr.ItemArray[iImg])
                                                    ,Convert.ToBoolean(dr.ItemArray[iEna])
                                                    ,Convert.ToBoolean(dr.ItemArray[iVis]));                        
                        this.AppCode = dr.ItemArray[iApp].ToString().Trim().ToUpper();
                        //if ((this.AppCode != "") && (mi.URL != ""))
                        //    AccordionMenuSearch.addItemToDictionary(AppCode, mi.URL + ";" + mi.Abbreviation);
                        //if ((this.AppCode != "") && ((mi.RequiredTokens != "") || (mi.SpecialFunctions != "")))
                        //    AccordionMenuSearch.addItemToTokenDictionary(AppCode, mi.RequiredTokens, mi.SpecialFunctions);
                        this.MenuItems.Add(mi);
                    }

                    if (ds.Tables.Count > 1)
                    {
                        //First Elements
                        iID = ds.Tables[1].Columns["ID"].Ordinal;
                        iURL = ds.Tables[1].Columns["URL"].Ordinal;
                        iTarget = ds.Tables[1].Columns["Target"].Ordinal;                       
                        iEna = ds.Tables[1].Columns["Enabled"].Ordinal;
                        iApp = ds.Tables[1].Columns["AppCode"].Ordinal;
                        iNam = ds.Tables[0].Columns["Name"].Ordinal;
                        iVis = ds.Tables[1].Columns["Visible"].Ordinal;                        
                        int iParent = ds.Tables[1].Columns["Parent"].Ordinal;
                        iImg = ds.Tables[1].Columns["Image"].Ordinal;


                        foreach (DataRow dr in ds.Tables[1].Rows)
                        {
                            MenuDefinition md = new MenuDefinition( Convert.ToInt32(dr.ItemArray[iID])
                                                                    ,Convert.ToString(dr.ItemArray[iApp])
                                                                    ,Convert.ToString(dr.ItemArray[iURL]).Trim()
                                                                    ,Convert.ToString(dr.ItemArray[iNam]).Trim()
                                                                    ,Convert.ToString(dr.ItemArray[iTarget]).Trim()
                                                                    ,Convert.ToString(dr.ItemArray[iImg])
                                                                    ,Convert.ToBoolean(dr.ItemArray[iEna])
                                                                    ,Convert.ToBoolean(dr.ItemArray[iVis]));
                            int parent = Convert.ToInt32(dr.ItemArray[iParent]);
                            MenuItem mi = this.getMenuItem(parent);
                            AppCode = dr.ItemArray[iApp].ToString().Trim().ToUpper();
                            //if ((AppCode != "") && (md.URL != ""))
                            //    AccordionMenuSearch.addItemToDictionary(AppCode, md.URL + ";" + md.Abbreviation);
                            //if ((AppCode != "") && ((md.RequiredTokens != "") || (md.SpecialFunctions != "")))
                            //    AccordionMenuSearch.addItemToTokenDictionary(AppCode, md.RequiredTokens, md.SpecialFunctions);
                            if (mi != null)
                                mi.MenuDefinitions.Add(md);
                        }

                        if (ds.Tables.Count > 2)
                        {
                            //Second Elements
                            iID = ds.Tables[2].Columns["ID"].Ordinal;
                            iURL = ds.Tables[2].Columns["URL"].Ordinal;
                            iTarget = ds.Tables[2].Columns["Target"].Ordinal;                            
                            iEna = ds.Tables[2].Columns["Enabled"].Ordinal;
                            iParent = ds.Tables[2].Columns["Parent"].Ordinal;
                            iApp = ds.Tables[2].Columns["AppCode"].Ordinal;
                            iImg = ds.Tables[2].Columns["Image"].Ordinal;
                            iVis = ds.Tables[2].Columns["Visible"].Ordinal;                            
                            iNam = ds.Tables[2].Columns["Name"].Ordinal;

                            foreach (DataRow dr in ds.Tables[2].Rows)
                            {
                                PopupItem pi = new PopupItem(   Convert.ToInt32(dr.ItemArray[iID])
                                                                ,Convert.ToString(dr.ItemArray[iApp])
                                                                ,Convert.ToString(dr.ItemArray[iURL]).Trim()
                                                                ,Convert.ToString(dr.ItemArray[iNam]).Trim()
                                                                ,Convert.ToString(dr.ItemArray[iTarget]).Trim()
                                                                ,Convert.ToString(dr.ItemArray[iImg]).Trim()
                                                                ,Convert.ToBoolean(dr.ItemArray[iEna])
                                                                ,Convert.ToBoolean(dr.ItemArray[iVis]));

                                int parent = Convert.ToInt32(dr.ItemArray[iParent]);
                                MenuDefinition md = getMenuDefinition(parent);
                                AppCode = dr.ItemArray[iApp].ToString().Trim().ToUpper();
                                //if ((AppCode != "") && (pi.URL != ""))
                                //    AccordionMenuSearch.addItemToDictionary(AppCode, pi.URL + ";" + pi.Abbreviation);
                                //if ((AppCode != "") && ((pi.RequiredTokens != "") || (pi.SpecialFunctions != "")))
                                //    AccordionMenuSearch.addItemToTokenDictionary(AppCode, pi.RequiredTokens, pi.SpecialFunctions);
                                if (md != null)
                                    md.PopupItems.Add(pi);
                            }
                            if (ds.Tables.Count > 3)
                            {
                                iID = ds.Tables[3].Columns["ID"].Ordinal;
                                iURL = ds.Tables[3].Columns["URL"].Ordinal;
                                iTarget = ds.Tables[3].Columns["Target"].Ordinal;
                                iEna = ds.Tables[3].Columns["Enabled"].Ordinal;
                                iParent = ds.Tables[3].Columns["Parent"].Ordinal;
                                iApp = ds.Tables[3].Columns["AppCode"].Ordinal;
                                iImg = ds.Tables[3].Columns["Image"].Ordinal;
                                iVis = ds.Tables[3].Columns["Visible"].Ordinal;
                                iNam = ds.Tables[3].Columns["Name"].Ordinal;
                                foreach (DataRow dr in ds.Tables[3].Rows)
                                {
                                    PopupItem subpi = new PopupItem(     Convert.ToInt32(dr.ItemArray[iID])
                                                                        ,Convert.ToString(dr.ItemArray[iApp])
                                                                        ,Convert.ToString(dr.ItemArray[iURL]).Trim()
                                                                        ,Convert.ToString(dr.ItemArray[iNam]).Trim()
                                                                        ,Convert.ToString(dr.ItemArray[iTarget]).Trim()
                                                                        ,Convert.ToString(dr.ItemArray[iImg]).Trim()
                                                                        ,Convert.ToBoolean(dr.ItemArray[iEna])
                                                                        ,Convert.ToBoolean(dr.ItemArray[iVis]));
                                    int parent = Convert.ToInt32(dr.ItemArray[iParent]);
                                    PopupItem parentPi = getPopupItem(parent);
                                    AppCode = dr.ItemArray[iApp].ToString().Trim().ToUpper();
                                    //if ((AppCode != "") && (subpi.URL != ""))
                                    //    AccordionMenuSearch.addItemToDictionary(AppCode, subpi.URL + ";" + subpi.Abbreviation);
                                    //if ((AppCode != "") && ((subpi.RequiredTokens != "") || (subpi.SpecialFunctions != "")))
                                    //    AccordionMenuSearch.addItemToTokenDictionary(AppCode, subpi.RequiredTokens, subpi.SpecialFunctions);
                                    if (parentPi != null)
                                        parentPi.SubPopupItems.Add(subpi);
                                }
                            }//end SubItems
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(strSource, "", ex);
                Logger.Error(strSource, "", ex);
                throw new Exception("FillSidebarMenu.  Error: " + ex.Message);
            }
        }        
        #endregion
        #region "Implements"
        public object GenerateResponse(DataSet ds)
        {
            IndustrialSidebarMenu sdm = new IndustrialSidebarMenu(ds);
            return (Object)sdm;
        }
        public object GetExcelChunk(System.IO.Stream document)
        {
            Logger.Warn(strSource, "GetExcelChunk() [NotImplementedException]");
            throw new NotImplementedException();
            //return null;
        }
        public object GetPDFChunk(System.IO.Stream document)
        {
            Logger.Warn(strSource, "GetPDFChunk() [NotImplementedException]");
            throw new NotImplementedException();
            //return null;
        }
        #endregion
    }
}
